"use strict";
cc._RF.push(module, 'b6cddnT2F9O9qvJY77Havl2', 'home');
// Script/2048/home.js

"use strict";

var _common = require("../common");

cc.Class({
  "extends": cc.Component,
  properties: {
    RankNode: cc.Node,
    AuthButton: cc.Node
  },
  onLoad: function onLoad() {
    this.applyUserAuth(); //预加载场景

    cc.director.preloadScene("game", function () {});
  },

  /**
   * 请求授权信息
   */
  applyUserAuth: function applyUserAuth() {
    console.log("请求授权信息");
    var self = this;
    var wx = window['wx'];
    wx.login({
      success: function success(res) {
        console.log(res);
        var code = res.code;
        wx.getSetting({
          success: function success(res) {
            //用户已经授权
            if (res.authSetting["scope.userInfo"]) {
              wx.getUserInfo({
                success: function success(res) {
                  console.log("已经授权");
                  console.log(res);
                  _common.G.is_login = true; // self.DrawUserInfo(res.userInfo); //画用户信息(微信的信息)
                  // self.GetToken(res,code);//获取TOKEN（请求API的信息）
                }
              });
            } else {
              console.log("未授权");
            }
          }
        });
      }
    });
  },

  /**
   * 创建点击授权按钮
   * @param {*} btnNode 
   */
  createAuthorizeBtn: function createAuthorizeBtn(btnNode) {
    var btnSize = cc.size(btnNode.width + 10, btnNode.height + 10);
    var frameSize = cc.view.getFrameSize();
    var winSize = cc.director.getWinSize(); //适配不同机型来创建微信授权按钮

    var left = (winSize.width * 0.5 + btnNode.x - btnSize.width * 0.5) / winSize.width * frameSize.width;
    var top = (winSize.height * 0.5 - btnNode.y - btnSize.height * 0.5) / winSize.height * frameSize.height;
    var width = btnSize.width / winSize.width * frameSize.width;
    var height = btnSize.height / winSize.height * frameSize.height;
    var self = this;
    self.btnAuthorize = wx.createUserInfoButton({
      type: 'text',
      text: '',
      style: {
        left: left,
        top: top,
        width: width,
        height: height,
        lineHeight: 0,
        backgroundColor: '',
        color: '#ffffff',
        textAlign: 'center',
        fontSize: 16,
        borderRadius: 4
      }
    });
    self.btnAuthorize.onTap(function (uinfo) {
      console.log("onTap uinfo: ", uinfo);

      if (uinfo.userInfo) {
        console.log("wxLogin auth success");
        wx.showToast({
          title: "授权成功"
        });
        _common.G.is_login = true;
      } else {
        console.log("wxLogin auth fail");
        wx.showToast({
          title: "授权失败",
          'icon': 'error'
        });
      }
    });
  },
  OnclickBegin: function OnclickBegin() {
    cc.director.loadScene("game");
  },
  OnclickRank: function OnclickRank(e, str) {
    if (str == "open") {
      this.RankNode.active = true;

      if (!_common.G.is_login) {
        this.AuthButton.active = true;
        this.createAuthorizeBtn(this.AuthButton);
      } else {
        this.AuthButton.active = false;
      }
    } else {
      this.RankNode.active = false;
      this.btnAuthorize.destroy();
    }
  },
  OnclickSetting: function OnclickSetting() {
    var _this = this;

    Alert.show("分享好友可得能量！", function () {
      _this.click();
    });
  },
  click: function click() {
    console.log('alertttttt');
  }
});

cc._RF.pop();